from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from .models import Area
from .serializers import AreaSerialzier,SubsAreaSerializer
from rest_framework_extensions.cache.mixins import ListCacheResponseMixin,RetrieveCacheResponseMixin
from rest_framework_extensions.cache.mixins import CacheResponseMixin
# Create your views here.

class AreaView(CacheResponseMixin,ReadOnlyModelViewSet):
    """
    #省份信息 select * from tb_areas where parent_id is null;

    # 市 select * from tb_areas where parent_id=130000;
    #区县 select * from tb_areas where parent_id=130600;


    # GET   省份信息的url      : areas/infos/          list
    # GET   市/区县的url信息    :areas/infos/pk/        retrieve
    """

    pagination_class = None
    #设置数据查询结果集
    # queryset = Area.objects.all() #省市区数据都返回了,

    def get_queryset(self):
        #重写这个方法,根据 不同的业务逻辑返回不同的查询数据
        # list,retrieve,create,update 等等都是 action
        if self.action == 'list':
            #返回省份信息
            return Area.objects.filter(parent__isnull=True)
        else:
            # 区县信息
            # 会根据 url中传递的pk来获取指定数据
            return Area.objects.all()



    # serializer_class =

    def get_serializer_class(self):

        if self.action == 'list':
            return AreaSerialzier
        else:
            return SubsAreaSerializer




# class ShengfenView():
#     def get(self,request):
# class ShiquxianView():
#     def get(self,request):
